//
//  RunModeViewController.m
//  RONGAO
//
//  Created by wei wang on 2017/2/23.
//  Copyright © 2017年 wei wang. All rights reserved.
//

#import "RunModeViewController.h"
#import "DoorSendCodeModel.h"
#import "BlueContentModel.h"
#import "RongGaoGit.h"
#import "AppDelegate.h"
@interface RunModeViewController ()<UITableViewDataSource,UITableViewDelegate>
/** tableView **/
@property(nonatomic,strong) UITableView *tableView;

/** titleArray **/
@property(nonatomic,strong) NSArray *titleArray;

/** lilteArray **/
@property(nonatomic,strong) NSArray *lilteArray;



@end

@implementation RunModeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavigationStyle];
    [self masLayoutSubview];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(refreshEvent) name:@"updateTableView" object:nil];
}

- (void)refreshEvent{
    
    [self.tableView reloadData];
    
}

- (void)setNavigationStyle{
    
    [self setNavigationTitle:@"运行模式" titleColor:nil];
    //    [self setRightButtonTitle:@"保存"];
    [self setLeftButtonImage:[UIImage imageNamed:@"icon_back_nor"]];
}

//发送指令 并保存数据到本地
- (void)sendCodeWithIndexPath:(NSInteger)row{
    
    DoorSendCodeModel *codeModel = [[DoorSendCodeModel alloc]init];
    [codeModel repairZero:[NSString stringWithFormat:@"%ld",(long)row+1]];
    
    [[AppDelegate globalDelegate] sendCommandByFunctionCode:@"100C" functionCommand:[codeModel getSendCmd]];



}


#pragma mark - --------------------------UITableView dataSource--------------------------

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.titleArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //读model
    //indexPath != MODEL.MODE
    DoorStatusModel *statusModel = [RongGaoGit getDoorStatusModel];
    NSLog(@"运行模式%@",statusModel.runMode);
    
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        cell.textLabel.textColor = [UIColor colorWithHexString:@"#56504f"];
        [cell.textLabel setFont:[UIFont systemFontOfSize:15.f]];
        cell.detailTextLabel.textColor = [UIColor colorWithHexString:@"#84807f"];
        [cell.detailTextLabel setFont:[UIFont systemFontOfSize:13.f]];
    }
    cell.textLabel.text = self.titleArray[indexPath.row];
    cell.detailTextLabel.text = self.lilteArray[indexPath.row];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
    
    cell.accessoryView = imageView;
    
    imageView.image = indexPath.row+1 == [statusModel.runMode integerValue] ? [UIImage imageNamed:@"video_icon_selected-"] : [UIImage imageNamed:@"video_icon_notchoose-"];
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 56;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return CGFLOAT_MIN;
    
}
#pragma mark - --------------------------UITableView delegate--------------------------

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //发送指令并保存
    [self sendCodeWithIndexPath:indexPath.row];
    [self.tableView reloadData];
    
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        __weak UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [superView addSubview:_tableView];
        //        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;//隐藏tableView的分割线
        //        _tableView.scrollEnabled = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
    }
    
    return _tableView;
    
}

- (NSArray *)titleArray{
    
    if (!_titleArray) {
        
        _titleArray = @[@"手动模式",@"自动模式"];
    }
    return _titleArray;
}

- (NSArray *)lilteArray{
    
    if (!_lilteArray) {
        
        _lilteArray = @[@"按一次开关只做开关或者关门",@"开关和感应器都可以开门"];
    }
    return _lilteArray;
}

- (void)masLayoutSubview{
    
    __weak UIView *superView = self.view;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(superView);
        
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    
}

@end
